package models

import (
	"github.com/beego/beego/v2/client/orm"
	"weiboxs.com/new_h_glasses/utils"
)

type ErpProductsAttribute struct {
	Id           int64
	OptionName   string
	OptionKey  string
	OptionContent  string
	Typeid       int
	Pinyin       string
	Status       int
}

type ErpProductsAttributeParam struct {
	BaseQueryParam
	Status string
}

func ErpProductsAttributeTableName() string {
	return TableName("erp_prodocts_attribute")
}

func (a *ErpProductsAttribute) TableName() string {
	return ErpProductsAttributeTableName()
}

func ErpProductsAttributeList(params *ErpProductsAttributeParam) []*ErpProductsAttribute {
	query := orm.NewOrm().QueryTable(ErpProductsAttributeTableName())
	lists := make([]*ErpProductsAttribute, 0)
	if len(params.Status) > 0 {
		query = query.Filter("status", params.Status)
	}
	query.All(&lists)
	return lists
}

func ErpProductsAttributeGetById(id int) (*ErpProductsAttribute, error) {

	r := new(ErpProductsAttribute)
	err := orm.NewOrm().QueryTable(ErpProductsAttributeTableName()).Filter("id", id).One(r)
	if err != nil {
		return nil, err
	}

	return r, nil
}

func ErpProductsAttributeAdd(a *ErpProductsAttribute) (int64, error) {
	a.Pinyin = utils.ConvertPinyin(a.OptionName)
	return orm.NewOrm().Insert(a)
}

func (a *ErpProductsAttribute) Update(fields ...string) error {
	a.Pinyin = utils.ConvertPinyin(a.OptionName)
	if _, err := orm.NewOrm().Update(a, fields...); err != nil {
		return err
	}
	return nil
}
